package communicationMessages;

import java.util.logging.Level;

import serverComuncation.Observer;

import database.DB;

import logger.LoggerWriter;

public class ChangeActiveModeMessage extends Message{

	@Override
	public String getAnswer() {
		String answer = null;
		String []arr = this.content.split(delimiter);
		LoggerWriter logger = LoggerWriter.getLoggerWriter();
		if (arr.length != 2)
		{
			logger.log("failed"+"changeActiveModeMessage wrong format"+delimiter, Level.WARNING);
			return "failed"+delimiter+"changeActiveModeMessage wrong format"+delimiter;
		}
		else{
			answer = "ChangeActiveMode" + delimiter;
			String userName = arr[0];//operate
			String state = arr[1];
			boolean mode = Boolean.valueOf(state);
			System.out.println("the state is " + state + " the mode boolean is " + mode);
			int isActive = getMode(mode);
			System.out.println("is active is " + isActive);
			DB.changeMode(userName,isActive);
			if (mode)
			{
				Observer.observer.activateUser(Thread.currentThread().getId(), userName);
				answer += "User "+userName +" became Active ";
			}
			else
			{
				Observer.observer.unActivateUser(Thread.currentThread().getId());
				answer += "User "+userName +" became UNActive ";

			}
		}
		return answer;
	}

	private int getMode(boolean mode) {
		return mode==true ? 1:0;
	}

}
